package rs.dalao.syc.doctor.all.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import rs.dalao.syc.common.ex.ServiceException;
import rs.dalao.syc.common.pojo.authentication.CurrentPrincipal;
import rs.dalao.syc.common.response.JsonResult;
import rs.dalao.syc.common.response.ServiceCode;
import rs.dalao.syc.doctor.all.pojo.param.DoctorCredentialParam;
import rs.dalao.syc.doctor.all.pojo.param.DoctorUpdateParam;
import rs.dalao.syc.doctor.all.pojo.vo.DoctorCredentialVO;
import rs.dalao.syc.doctor.all.pojo.vo.DoctorInfoVO;
import rs.dalao.syc.doctor.all.pojo.vo.UploadResult;
import rs.dalao.syc.doctor.all.pojo.vo.UserPersonalListVO;
import rs.dalao.syc.doctor.all.service.IDoctorCredentialService;
import rs.dalao.syc.doctor.all.service.IDoctorService;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/doctor/info/")
@Api(tags = "医生信息")
public class DoctorController {
    @Autowired
    private IDoctorCredentialService doctorCredentialService;

    @Autowired
    private IDoctorService doctorService;

    @GetMapping("/list")
    @ApiOperation("展示医生证书信息")
    @ApiOperationSupport(order = 240)
    public JsonResult list(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal){
        DoctorCredentialVO doctorCredentialVO = doctorCredentialService.selectById(currentPrincipal.getId());
        return JsonResult.ok(doctorCredentialVO);
    }

    @GetMapping("/listInfo")
    @ApiOperation("展示医生个人信息")
    @ApiOperationSupport(order = 240)
    public JsonResult listInfo(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal){
        DoctorInfoVO doctorInfoVO  = doctorService.selectById(currentPrincipal.getId());
        return JsonResult.ok(doctorInfoVO);
    }

    @PostMapping("/insert")
    @ApiOperation("新增或者修改用户的证书信息")
    @ApiOperationSupport(order = 250)
    public JsonResult insert(DoctorCredentialParam doctorCredentialParam,
                             @AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal){
        int insert = doctorCredentialService.insert(doctorCredentialParam, currentPrincipal.getId());
        return JsonResult.ok(insert);
    }

    //删除头像
    @PostMapping("/remove")
    @ApiOperation("删除医生头像")
    public JsonResult remove(String url){
        //删除对应的磁盘中的文件
        /* new File("d:/files"+url).delete();*/
        boolean isDelete = new File(url).delete();
        return JsonResult.ok(isDelete);
    }

    //更改用户用户头像、昵称信息
    @PostMapping("/update")
    @ApiOperation("更新医生的个人资料")
    public JsonResult update(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal, DoctorUpdateParam doctorUpdateParam){
        doctorService.updateDoctorInfo(doctorUpdateParam,currentPrincipal.getId());
        return JsonResult.ok();
    }

    //展示聊天用户的真实信息
    @PostMapping("list/{userName}")
    @ApiOperation("根据用户名展示用户真实信息")
    @PreAuthorize("hasAuthority('/doc/prescription/patientList')")
    public JsonResult ListInfo(@PathVariable String userName){
        UserPersonalListVO userPersonalListVO = doctorService.selectUserInfoByUsername(userName);
        return JsonResult.ok(userPersonalListVO);
    }
}
